package jtk.project4.fleet.task;

import jtk.project4.fleet.domain.BrowsePayments;
import jtk.project4.fleet.domain.Invoices;
import jtk.project4.fleet.event.BrowsePaymentsEvent;
import jtk.project4.fleet.event.InvoiceEvent;
import jtk.project4.fleet.event.BrowsePaymentsEvent.BrowsePaymentsType;
import jtk.project4.fleet.event.InvoiceEvent.InvoiceEventType;
import jtk.project4.fleet.service.DatabaseService;

import java.util.List;

import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

public class LoadInvoicesTask extends Task<List<Invoices>>{
	
	@Override
	public List<Invoices> process() throws Exception{
		return DatabaseService.getInstance().getInvoices();
	}
	
	@Override
	protected void onStart() {
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading contacts...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		BrowsePaymentsEvent invoicesEvent = new BrowsePaymentsEvent(BrowsePaymentsType.LOAD_SUCCESS);
//		browsePaymentsEvent.setBrowsePayments(invoices);
//		postEvent(browsePaymentsEvent);
//		InvoiceEvent invoicesEvent = new InvoiceEvent(InvoiceEventType.LOAD_SUCCESS);
//		invoicesEvent.setInvoices(getResult());
//		postEvent(invoicesEvent);
		invoicesEvent.setInvoices(getResult());
		postEvent(invoicesEvent);
	}
	
	@Override
	protected void onError() {
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}

}
